package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
)

type BusNovelsListService struct {
}

// CreateBusNovelsList 创建BusNovelsList记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsListService *BusNovelsListService) CreateBusNovelsList(busNovelsList autocode.BusNovelsList) (err error) {
	err = global.GVA_DB.Create(&busNovelsList).Error
	return err
}

// DeleteBusNovelsList 删除BusNovelsList记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsListService *BusNovelsListService)DeleteBusNovelsList(busNovelsList autocode.BusNovelsList) (err error) {
	err = global.GVA_DB.Delete(&busNovelsList).Error
	return err
}

// DeleteBusNovelsListByIds 批量删除BusNovelsList记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsListService *BusNovelsListService)DeleteBusNovelsListByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.BusNovelsList{},"id in ?",ids.Ids).Error
	return err
}

// UpdateBusNovelsList 更新BusNovelsList记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsListService *BusNovelsListService)UpdateBusNovelsList(busNovelsList autocode.BusNovelsList) (err error) {
	err = global.GVA_DB.Save(&busNovelsList).Error
	return err
}

// GetBusNovelsList 根据id获取BusNovelsList记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsListService *BusNovelsListService)GetBusNovelsList(id uint) (err error, busNovelsList autocode.BusNovelsList) {
	err = global.GVA_DB.Where("id = ?", id).First(&busNovelsList).Error
	return
}

// GetBusNovelsListInfoList 分页获取BusNovelsList记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsListService *BusNovelsListService)GetBusNovelsListInfoList(info autoCodeReq.BusNovelsListSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&autocode.BusNovelsList{})
    var busNovelsLists []autocode.BusNovelsList
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Uuid != "" {
        db = db.Where("Uuid = ?",info.Uuid)
    }
    if info.Name != "" {
        db = db.Where("name LIKE ?","%"+ info.Name+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
	err = db.Limit(limit).Offset(offset).Find(&busNovelsLists).Error
	return err, busNovelsLists, total
}


func  (busNovelsListService *BusNovelsListService)DeleteAllBusNovelsList() () {
	global.GVA_DB.Raw("TRUNCATE TABLE bus_novels_list").Scan(&[]autocode.BusNovelsList{})
}